<template>
  <div class="race-page">
    <div class="header">
      <div class="round-info">
        <span class="round-text">第 {{ this.roundIndex }} 轮</span>
        <span class="timer">剩余时间: {{ minutes }}:{{ seconds }}</span>
      </div>
      <div class="btn">
        <el-button @click="nextRound" icon="el-icon-refresh"> </el-button>
        <!-- <el-button type="primary" @click="dialogVisible = true">确定</el-button> -->
      </div>
    </div>

    <el-divider />

    <!-- 管理员界面 -->
    <el-card
      v-if="role === 'sa' || role === 'teacher' || role === 'student'"
      class="admin-panel"
    >
      <div v-if="winner" class="winner-info">
        <el-alert
          title="抢答成功！"
          type="success"
          :description="`抢答者：${winner.name}`"
        />
        <div v-if="submittedAnswer" class="submitted-answer">
          <h3>考生提交的信息：</h3>
          <p>选中人员：{{ submittedAnswer.selectedPerson }}</p>
          <p>姓名：{{ submittedAnswer.name }}</p>
          <p>所住小区：{{ submittedAnswer.community }}</p>
        </div>
      </div>

      <!-- 显示辖区人员头像 -->
      <div class="people-grid">
        <div
          v-for="person in popQuList"
          :key="person.id"
          class="person-card"
          :class="{
            selected: selectPopQu.includes(person),
            disabled: selectedHistory.includes(person.id),
          }"
          @click="selectPerson(person)"
        >
          <div class="avatar-container">
            <img :src="person.image" alt="" class="avatar" />
          </div>
          <div class="serial-number">
            <el-tag size="medium" type="info">编号：{{ person.id }}</el-tag>
          </div>
        </div>
      </div>
    </el-card>
    <!-- <div class="next-round">
      <el-button class="dialog-btn" @click="dialogVisible = true">确定</el-button>
    </div> -->
    <el-card class="next-round">
      <el-button type="primary" class="dialog-btn" @click="dialogVisible = true"
        >确定</el-button
      >
    </el-card>

    <!-- <el-card style="margin: 10px 0;">
      <el-button type="primary" @click="()=>this.$router.go(-1)" icon="el-icon-arrow-left">
      </el-button>
      <el-button type="primary" @click="()=>this.$router.push({name:'Saftey'})" icon="el-icon-arrow-right" style="float: right;">
      </el-button>
    </el-card> -->

    <div class="btn-next">
      <el-button @click="() => this.$router.go(-1)" icon="el-icon-arrow-left">
      </el-button>
      <el-button
        @click="() => this.$router.push({ name: 'Saftey' })"
        icon="el-icon-arrow-right"
        style="float: right"
      >
      </el-button>
    </div>

    <el-dialog
      :visible.sync="dialogVisible"
      width="100%"
      :before-close="handleClose"
      :fullscreen="true"
      :center="true"
    >
      <el-row :gutter="20" type="flex" justify="center" align="middle">
        <el-col :span="12" v-for="item in selectPopQu" :key="item.id">
          <el-card class="comparison-card">
            <div class="image-container">
              <img :src="item.image" :alt="item.name" />
            </div>
            <div class="answer-container" v-if="showAnswer">
              <h2>社区：{{ item.address }}</h2>
              <h3>姓名： {{ item.name }}</h3>
              <p v-if="roundArr[roundIndex - 1] === 'important'">
                管控类型： {{ item.content }}
              </p>
              <p v-if="roundArr[roundIndex - 1] === 'important'">
                当前去向：{{ item.job }}
              </p>
            </div>
            <div class="dialog-serial-number">
              <el-tag size="medium" effect="dark" type="warning"
                >编号：{{ item.id }}</el-tag
              >
            </div>
          </el-card>
        </el-col>
      </el-row>
      <div class="dialog-footer">
        <el-button type="primary" @click="toggleAnswer">
          {{ showAnswer ? "隐藏答案" : "显示答案" }}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { saveRob, saveQuick } from "@/api/exam/exam";
import { fetchQucikDetail } from "@/api/paper/exam";
import { mapGetters } from "vuex";

export default {
  name: "Population",

  data() {
    return {
      role: "student",
      winner: null,
      submittedAnswer: null,
      popQuList: [],
      selectPopQu: [],
      dialogVisible: false,
      showAnswer: false,
      selectedHistory: [],
      roundArr: ["roundfz", "roundcz", "roundld", "important"],
      timer: null,
      timeLeft: 60, // 1分钟 = 60秒
      minutes: "00",
      seconds: "60",
      roundIndex: 0,
      community: [
        "白石警务区",
        "湘府社区",
        "湖大社区",
        "潭洲社区",
        "阳光里社区",
        "王十万社区",
        "永佳社区",
        "青山桥警务区",
        "长岭警务区",
        "长青社区",
        "东阳社区",
        "黄金二社区",
      ],
    };
  },

  created() {
    this.role = this.$store.state.user.roles[0];
    this.fetchData();
    this.startTimer();
  },

  computed: {
    ...mapGetters(["avatar"]),
  },

  methods: {
    startTimer() {
      this.timeLeft = 60;
      this.minutes = 0;
      this.seconds = 60;
      if (this.timer) {
        clearInterval(this.timer);
      }

      this.timer = setInterval(() => {
        this.timeLeft--;
        this.minutes = Math.floor(this.timeLeft / 60)
          .toString()
          .padStart(2, "0");
        this.seconds = (this.timeLeft % 60).toString().padStart(2, "0");

        if (this.timeLeft <= 0) {
          this.$message.warning("本轮时间已到!");
          clearInterval(this.timer);
        }
      }, 1000);
    },

    nextRound() {
      // this.roundIndex++
      console.log(this.roundArr[this.roundIndex]);
      if (this.roundIndex < this.roundArr.length) {
        this.fetchData();
        this.startTimer();
        // this.roundIndex++;
      } else {
        this.$message.warning("已经是最后一轮了");
      }
    },

    fetchData() {
      const params = { id: this.roundArr[this.roundIndex++] };
      console.log(params);
      fetchQucikDetail(params).then((res) => {
        // console.log(res);
        this.popQuList = res.data.popQu;
      });
    },

    selectPerson(person) {
      this.selectPopQu.push(person);
      if (this.selectPopQu.length > 2) {
        this.selectPopQu.shift();
      }
    },

    handleClose() {
      this.selectPopQu = [];
      this.dialogVisible = false;
      this.showAnswer = false;
    },

    toggleAnswer() {
      this.showAnswer = !this.showAnswer;
    },
  },

  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  },
};
</script>

<style scoped>
.race-page {
  padding: 20px;
  background-color: #f9f9f9;
  min-height: 100vh;

  width: 100%;
  /* height: 100%; */
  background-image: url("/images/bg.png");
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.header {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  /* margin-bottom: 20px; */
}

.round-info {
  display: flex;
  align-items: center;
  gap: 20px;
}

.round-text {
  font-size: 24px;
  font-weight: bold;
  /* color: #409eff; */
  color: white;
}

.timer {
  font-size: 24px;
  color: #f56c6c;
  font-weight: bold;
}

.el-divider {
  margin: 1px;
}

.btn {
  display: flex;
  justify-content: flex-end;
  opacity: 0.3;
}

.admin-panel {
  margin: 20px auto;
  max-width: 1200px;
  /* padding: 20px; */
  background-color: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
}

.people-grid {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 30px;
  margin: 10px 0;
}

.person-card {
  text-align: center;
  padding: 10px;
  border: 1px solid #eee;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.person-card.selected {
  border-color: #409eff;
  box-shadow: 0 0 10px rgba(64, 158, 255, 0.5);
}

.person-card.disabled {
  opacity: 0.5;
  cursor: not-allowed;
  background-color: #f5f5f5;
}

.avatar-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  margin-bottom: 10px;
}

.avatar {
  width: 100%;
  height: 100%;
  /* border-radius: 50%; */
  object-fit: cover;
}

.serial-number {
  margin-top: 8px;
  display: flex;
  justify-content: center;
}

.next-round {
  width: 1200px;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 10px;
  z-index: 10;
  /* height: 50px; */
  /* opacity: 0.8; */
}

.dialog-btn {
  /* color: #409eff; */
  width: 200px;
  font-size: 20px;
  margin: 0;
}

.winner-info {
  margin-top: 20px;
}

.submitted-answer {
  margin-top: 20px;
  padding: 10px;
  background-color: #f0f9eb;
  border: 1px solid #e1f3d8;
  border-radius: 4px;
}

.comparison-card {
  margin: 20px;
  text-align: center;
  padding: 20px;
}

.image-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 400px;
  margin: 20px 0;
}

.image-container img {
  max-width: 100%;
  max-height: 100%;
  object-fit: scale-down;
}

.answer-container {
  margin: 20px 0;
  padding: 15px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.answer-container h3 {
  color: #409eff;
  margin-bottom: 10px;
}

.answer-container p {
  color: #606266;
  line-height: 1.6;
  margin: 5px 0;
}

.dialog-footer {
  text-align: center;
  margin-top: 20px;
}

.dialog-serial-number {
  margin-top: 15px;
}

.btn-next {
  width: 97.6%;
  bottom: 20px;
  position: fixed;
  opacity: 0.3;
  z-index: 1;
}
</style>